package com.cxs.controller;

import com.cxs.entity.dto.ReservationSeatDTO;
import com.cxs.entity.SeatOrder;
import com.cxs.service.SeatOrderService;
import com.cxs.service.SeatService;
import com.cxs.entity.vo.AdminLoginVO;
import com.cxs.entity.vo.Result;
import com.cxs.entity.vo.SeatOrderVO;
import com.cxs.entity.vo.StudentLoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/*
 * @Project:seat-subscribe-sys
 * @Author:cxs
 * @Motto:放下杂念,只为迎接明天更好的自己
 * */
@Controller
@RequestMapping("/seat")
public class SeatController {

    @Autowired
    private SeatService seatService;

    @Autowired
    private SeatOrderService seatOrderService;

    @RequestMapping("/getSeatOrderInfo")
    @ResponseBody
    public Result getSeatOrderInfo(Integer id, HttpSession session) {
        Result r = new Result(200, "OK");
        seatService.getSeatOrderInfo(id, r);
        return r;
    }


    @RequestMapping("/reservationSeat")
    @ResponseBody
    public Result reservationSeat(@RequestBody ReservationSeatDTO dto, HttpSession session) {
        Result r = new Result(200, "OK");
        Object loginUser = session.getAttribute("loginUser");
        if (loginUser != null && loginUser instanceof StudentLoginVO) {
            StudentLoginVO stu = (StudentLoginVO) loginUser;
            seatService.reservationSeat(dto, stu.getSno(), r);
        } else {
            r.setCode(-1);
            r.setMsg("非学生不可预定,请不要浪费公共资源,谢谢!");
        }
        return r;
    }

    @RequestMapping("/deleteSeatOrder")
    @ResponseBody
    public Result deleteSeatOrder(Integer id, HttpSession session) {
        Result r = new Result(200, "OK");
        Object loginUser = session.getAttribute("loginUser");
        if (null != loginUser) {
            if (loginUser instanceof AdminLoginVO) {
                seatOrderService.removeById(id);
            } else {
                SeatOrder byId = seatOrderService.getById(id);
                if (byId != null) {
                    LocalDate date = byId.getDate();
                    Integer startTime = byId.getStartTime();
                    LocalDate now = LocalDate.now();
                    int hour = LocalDateTime.now().getHour();
                    if (now.equals(date) && startTime > hour) {
                        seatOrderService.removeById(id);
                    } else if (date.isAfter(now)) {
                        seatOrderService.removeById(id);
                    } else {
                        r.setCode(-1);
                        r.setMsg("该预定已过期,不可退订");
                    }
                } else{
                    r.setCode(-1);
                    r.setMsg("该预定已过期");
                }
            }
        } else{
            r.setCode(-1);
            r.setMsg("退订失败");
        }
        return r;
    }

    @RequestMapping("/toMyReservationInfo")
    public String toMyReservationInfo(HttpSession session, Model model) {
        Object loginUser = session.getAttribute("loginUser");
        if (loginUser != null && loginUser instanceof StudentLoginVO) {
            StudentLoginVO stu = (StudentLoginVO) loginUser;
            List<SeatOrderVO> list = seatOrderService.getReservationBySno(stu.getSno(), null);

            model.addAttribute("list", list);
        }
        return "student/myeservation";
    }
}
